<%@page import="models.Kategori" %>
<%@page import="java.util.ArrayList" %>
<%@page import="java.util.Map" %>
<%@page import="java.util.HashMap.*" %>
<%@page import="java.util.Iterator" %>

<%@ page  import="java.awt.*" %>
<%@ page  import="java.io.*" %>
<%@ page  import="org.jfree.chart.*" %>
<%@ page  import="org.jfree.chart.axis.*" %>
<%@ page  import="org.jfree.chart.entity.*" %>
<%@ page  import="org.jfree.chart.labels.*" %>
<%@ page  import="org.jfree.chart.plot.*" %>
<%@ page  import="org.jfree.chart.renderer.category.*" %>
<%@ page  import="org.jfree.chart.urls.*" %>
<%@ page  import="org.jfree.data.category.*" %>
<%@ page  import="org.jfree.data.general.*" %>

<% ArrayList<Kategori> result = (ArrayList<Kategori>) request.getAttribute("categories");%>

<script type="text/javascript">
    $(document).ready(function() {
        $("#addBarangForm").validate({
            rules : {
                nama_barang : {
                    required : true
                },
                harga_satuan : {
                    required : true,
                    number : true
                },
                deskripsi : {
                    required : true
                }
            },
            messages : {
                nama_barang : "x",
                harga_satuan : "x",
                deskripsi : "x"
            }
        });
    });
</script>

<h1>Statistik Toko</h1>

<div class="statistic_form">
    <form method="post" name="frm" action="<%=request.getContextPath()%>/statistik">
        <table border="0" width="400px" align="center" bgcolor="#CDFFFF">
            <tr>
                <td><b>Dari</b></td>
                <td>
                    <input  type="text" name="start_date" id="start_date" 
                            class="datepicker" value="<%= (request.getAttribute("startdate") != null ? request.getAttribute("startdate") : "")%>">
                </td>
            </tr>
            <tr>
                <td><b>Sampai</b></td>
                <td>
                    <input  type="text" name="end_date" id="end_date" 
                            class="datepicker" value="<%= (request.getAttribute("enddate") != null ? request.getAttribute("enddate") : "")%>">
                </td>
            </tr>
            <tr>
                <td><b>Kategori</b></td>
                <td>
                    <select onchange="" name="category">
                        <%
                                    if (result != null) {
                                        if (request.getAttribute("chosen_categ") != null) {
                                            if (request.getAttribute("chosen_categ").equals("All")) {%>
                        <option value="All" selected="true">Semua</option>
                        <% } else {%>
                        <option value="All">Semua</option>
                        <% }
                                                                } else {%>
                        <option value="All">Semua</option>
                        <% }
                                                                Kategori categ;
                                                                for (int i = 0; i < result.size(); ++i) {
                                                                    categ = result.get(i);
                                                                    if (request.getAttribute("chosen_categ") != null) {
                                                                        if (request.getAttribute("chosen_categ").equals("" + categ.id)) {%>
                        <option value="<%= categ.id%>" selected="true"><%= categ.nama%></option>
                        <% } else {%>
                        <option value="<%= categ.id%>"><%= categ.nama%></option>
                        <% }
                                                                                            } else {%>
                        <option value="<%= categ.id%>"><%= categ.nama%></option>
                        <% }
                                        }
                                    }
                        %>
                    </select>
                </td>
            </tr>
            <tr>
                <td><b>Status</b></td>
                <td>
                    <select onchange="" name="delivery_status">
                        <%
                                    int chosen_status = 0;
                                    if (request.getAttribute("chosen_status") != null) {
                                        chosen_status = (Integer) request.getAttribute("chosen_status");
                                        System.out.println("chosen_status : " + chosen_status);
                                    }

                                    if (chosen_status == 0) {%>
                        <option value="0" selected="true">Semua</option>
                        <% } else {%>
                        <option value="0">Semua</option>
                        <% }
                                    if (chosen_status == 1) {%>
                        <option value="1" selected="true">Belum Dibayar</option>
                        <% } else {%>
                        <option value="1">Belum Dibayar</option>
                        <% }
                                    if (chosen_status == 2) {%>
                        <option value="2" selected="true">Sudah Konfirmasi</option>
                        <% } else {%>
                        <option value="2">Sudah Konfirmasi</option>
                        <% }
                                    if (chosen_status == 3) {%>
                        <option value="3" selected="true">Sudah Dibayar</option>
                        <% } else {%>
                        <option value="3">Sudah Dibayar</option>
                        <% }
                                    if (chosen_status == 4) {%>
                        <option value="4" selected="true">Sudah Tiba</option>
                        <% } else {%>
                        <option value="4">Sudah Tiba</option>
                        <% }%>
                    </select>
                </td>
            </tr>
            <tr>
                <td></td>
                <td colspan=2 align="center"><input type="submit" name="submit" value="Bangkitkan Statistik"></td>
            </tr>
        </table>
    </form>

    <%
                if (request.getAttribute("result") != null) {
    %>
    <div id="statistic_table">
        <form method="post" name="frm" action="<%=request.getContextPath()%>/statistik/graphics">
            <table border="0" width="400px" align="center" bgcolor="#CDFFFF">
                <tr>
                    <td>
                        <b>Nama Barang</b>
                    </td>
                    <td>
                        <b>Jumlah</b>
                    </td>
                </tr>
                <%
                                    Map barang_barang = (Map) request.getAttribute("result");
                                    Iterator iter = barang_barang.entrySet().iterator();

                                    while (iter.hasNext()) {
                                        Map.Entry item = (Map.Entry) iter.next();
                %>
                <tr>
                    <td>
                        <%= (String) item.getKey()%>
                    </td>
                    <td>
                        <%=(Integer) item.getValue()%>
                    </td>
                </tr>
                <%
                                    }
                %>
            </table>
        </form>
    </div>
    <div style="width: auto;margin-left: auto; margin-right: auto; text-align: center">
        <img src="<%=request.getContextPath() + "/upload/chart.jpg"%>" alt="">
    </div>
    <%
                    } else if (request.getAttribute("result") == null){
    %>
    <div>Data tidak ditemukan</div>
    <%}
    %>
    <%  /*
                final double[][] data = new double[][]{
                {210, 300, 320, 265, 299},
                {200, 304, 201, 201, 340}
                };

                final CategoryDataset dataset =
                DatasetUtilities.createCategoryDataset("Team ", "", data);

                JFreeChart chart = null;
                BarRenderer renderer3D = null;
                CategoryPlot plot = null;


                final CategoryAxis3D categoryAxis = new CategoryAxis3D("Match");
                final ValueAxis valueAxis = new NumberAxis3D("Run");
                renderer3D = new BarRenderer3D();

                plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer3D);
                plot.setOrientation(PlotOrientation.VERTICAL);
                chart = new JFreeChart("Srore Bord", JFreeChart.DEFAULT_TITLE_FONT,
                plot, true);

                chart.setBackgroundPaint(new Color(249, 231, 236));

                try {
                final ChartRenderingInfo info = new ChartRenderingInfo
                (new StandardEntityCollection());
                final File file1 = new File("E:/3dbarchart.png");
                ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
                } catch (Exception e) {
                out.println(e);
                }  */
    %>
    <!--
          <IMG SRC="E:/3dbarchart.png"
            WIDTH="600" HEIGHT="400" BORDER="0" USEMAP="#chart">  -->
</div>

<script type="text/javascript">
    $(function() {
        $( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });

    });
</script>
